import tkinter as tk
from tkinter import ttk

class AlgorithmControlPanel(ttk.Frame):
    def __init__(self, parent, update_preset_callback, start_callback, stop_callback):
        super().__init__(parent)
        self.update_preset_callback = update_preset_callback
        self.start_callback = start_callback
        self.stop_callback = stop_callback
        self.create_widgets()

    def create_widgets(self):
        algo_group = ttk.LabelFrame(self, text="算法参数")
        algo_group.pack(fill=tk.X, pady=5)
        
        ttk.Label(algo_group, text="速度预设:").pack(anchor=tk.W)
        self.speed_var = tk.StringVar(value="快速")
        ttk.Radiobutton(algo_group, text="快速评估（迭代50次，约5分钟）", variable=self.speed_var, value="快速", 
                       command=self.update_preset_callback).pack(anchor=tk.W)
        ttk.Radiobutton(algo_group, text="中精度（迭代200次，约1小时）", variable=self.speed_var, value="中速", 
                       command=self.update_preset_callback).pack(anchor=tk.W)
        ttk.Radiobutton(algo_group, text="高精度（迭代500次，超过4个小时）", variable=self.speed_var, value="慢速", 
                       command=self.update_preset_callback).pack(anchor=tk.W)

        # Control buttons
        btn_frame = ttk.Frame(self)
        btn_frame.pack(fill=tk.X, pady=10)
        
        self.speed_controls = [
            algo_group.winfo_children()[1], 
            algo_group.winfo_children()[2],
            algo_group.winfo_children()[3]
        ]
        
        self.run_btn = ttk.Button(btn_frame, text="运行", command=self.start_callback)
        self.stop_btn = ttk.Button(btn_frame, text="停止", command=self.stop_callback)
        self.run_btn.pack(side=tk.LEFT, padx=(0, 5))
        self.stop_btn.pack(side=tk.LEFT, padx=(5, 0))
        
        # 初始化按钮状态
        self.stop_btn.config(state=tk.NORMAL)

    def enable_speed_preset(self):
        """启用速度预设控件"""
        for control in self.speed_controls:
            control.config(state=tk.NORMAL)
            
    def enable_run_button(self):
        """启用运行按钮并禁用停止按钮"""
        self.run_btn.config(state=tk.NORMAL)
        self.stop_btn.config(state=tk.DISABLED)
        
    def disable_run_button(self):
        """禁用运行按钮并启用停止按钮"""
        self.run_btn.config(state=tk.DISABLED)
        self.stop_btn.config(state=tk.NORMAL)

    def disable_speed_preset(self):
        """禁用速度预设控件"""
        for control in self.speed_controls:
            control.config(state=tk.DISABLED)
